﻿using System;
using System.Collections.Generic;
using System.Linq;
using DataLayer;
using Common;
using Common.DTO;

namespace LogicLayer
{
    public class PlayerHandler
    {

        public static Boolean CheckPlayer(string namn)
        {
            return CheckDBInfo.CheckIfPlayerExits(namn);
        }

        public static Boolean SetAndControlPlayerName(string namn)
        {
                if (namn == string.Empty)
                    return false;

           return CheckDBInfo.CheckIfPlayerExits(namn) ? false : SaveDBInfo.SavePlayerNameNameInfoDB(namn);
        }

        public static List<Player> GetPlayerList()
        {
            var spelarna = GetDBInfo.GetAllPlayers();

            return spelarna.Select(sp => new Player{ID = sp.ID, Name = sp.Name}).ToList();
        }

        public static bool AddPlayerSession(int playerId, int sessionId)
        {
            try
            {
                //var player = GetDBInfo.GetPlayerById(playerId);
                //var session = GetDBInfo.GetGameSessionById(sessionId);
                if (SaveDBInfo.AddPlayerSession(playerId, sessionId))
                {
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }

        public static bool MakePlayerChoice(int gameSessionId, int playerId, int playerChoiceId)
        {
            return SaveDBInfo.MakePlayerChoice(gameSessionId, playerId, playerChoiceId);
        }

        public static Player LoginPlayer(string login, string password)
        {
            try
            {
                var player = GetDBInfo.LoginPlayer(login, password);

                return new Player { ID = player.ID, Name = player.Name };
            }
            catch
            {
                new Exception("Player not found");
            }

            return null;
        }

        public static Player GetOpponent(int sessionId, int playerId)
        {
            var opponentId = GetDBInfo.GetOpponentIdFromSession(sessionId, playerId);

            return GetDBInfo.GetPlayerById(opponentId);
        }
    }
}
